package org.vectrics.user;

import java.util.Collection;
import java.util.HashMap;

/**
 * Clase que representa el Genero de la Persona.
 * @author mfunes
 *
 */
public final class Gender {
	
    public static final String GENDER_MALE = "m";
	public static final String GENDER_FEMALE = "f";
	public static final String GENDER_NOT_GIVEN = "n";
	
	public static final Gender MALE = new Gender(GENDER_MALE, "Male");
	public static final Gender FEMALE = new Gender(GENDER_FEMALE, "Female");
	public static final Gender NOT_GIVEN = new Gender(GENDER_NOT_GIVEN, "Not Given");
	
	private String code = GENDER_NOT_GIVEN;
	private String description = null;
	@SuppressWarnings("unchecked")
	private static HashMap map = null;

	/**
	 * Obtiene el Genero de acuerdo al codigo pasado.
	 * @param code
	 * @return La clase Gender correspondiente.
	 */
    public static Gender find(String code) {
	    loadMap();
		return((Gender) map.get(code));
	}
    
    /**
     * Carga el map con los generos mas comunes.
     * Male, Female y la opcion de Not given.
     */
    @SuppressWarnings("unchecked")
	private static void loadMap(){
	    if(map == null){
    	    map = new HashMap<String, Gender>();
            map.put(GENDER_MALE, MALE);
            map.put(GENDER_FEMALE, FEMALE);
            map.put(GENDER_NOT_GIVEN, NOT_GIVEN);
	    }
	}
	
    @SuppressWarnings("unchecked")
	public static Collection findAll() {
	    loadMap();
		return(map.values());
	}
	
    @SuppressWarnings("unchecked")
	private Gender(String code, String description) {
		super();
		this.code = code;
		this.description = description;
		loadMap();
		map.put(code, this);
	}
	
	public String toString() {
		return(description);
	}

	public String getCode() {
		return code;
	}

	public String getDescription() {
		return description;
	}
	
	public boolean equals(Gender other) {
		return(this.getCode().equals(other.getCode()));
	}

	public int hashCode() {
		return(this.getCode().hashCode());
	}
}
